﻿<!DOCTYPE HTML >
<!-- run this with winsock http server.cpp-->
<html>
<head>
    <title>winsock client</title>
    <link href="js/jqplot/jquery.jqplot.css" rel="stylesheet" type="text/css" />
    <script src="js/jqplot/jquery.js" type="text/javascript"></script>    
    <script class="code" type="text/javascript">

        $(document).ready(function () {
        var supplier = {
        ContactName : "new supplier", 
        City : "San Francisco",
        PoastalCode : "90001",
        Country : "USA",
        };
        var json = JSON.stringify(supplier);
        var html = "<p>" + json + "</p>";
        $("#out").html(html);

            $("#get").click(function (e) {                
                $.ajax({
                    type: "GET",                    
                    url: baseAddress + "/supplier/post",
                    data: { json: json },
                    success: function (response) {
                        if (typeof response != "undefined")
                            $("#out").append("HTTP GET response:<br/>" + response + "<br/>");
                    }
                });
            });
            $("#post").click(function (e) {
                  //POST as JSON object (WebInvoke)            
            $.ajax({
                type: "POST",                
                url: baseAddress + "/supplier/bad/post",
                data: { s: obj },
                success: function (response) {
                    if (typeof response != "undefined")
                        $("#out").append("HTTP POST response:<br/>" + response + "<br/>");
                }
            });   
            });
            
        });

        var baseAddress = "http://127.0.0.1:38950/NorthwindService.svc";
      
        //object if parses, (bool)false if fails to parse.
        function tryParseJSON(json) {
            try {
                var result = eval("(" + json + ")");
                if (typeof result == "object")
                    return result;
            }
            catch (e) {
            }
            return json;
        } 
    </script>
</head>
<body>
    <div style="font-family: verdana; font-size: 9.5px;">
        <aside>
This demonstrates send/receive-ing JSON between jQuery/javascript and a WCF Web HTTP (REST) service.<br />
GET: send Supplier as JSON string with HTTP GET (WebGet).<br />
POST: HTTP POST Supplier as JSON object<br />
</aside>
        <div>
            <h3>
                Supplier</h3>
            <br />            
            <button id="get">GET</button>
            <button id="post">POST</button>
        </div>
        <div>
        <output id="out" />
        </div>
    </div>
</body>
</html>
